package com.hsc.www;


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@Controller
public class ConfigServer5010 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServer5010.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;


    @RequestMapping("/")
    public String step1() {
        String s = "redirect:https://github.com/login/oauth/authorize?client_id=c624d2db46996072baa3&scope=user";
        return s;
    }

    @RequestMapping("/callback")
    @ResponseBody
    public Object step2(String code) {
        System.out.println("code=" + code);
        JSONObject jsonObject = restTemplate.getForObject("https://github.com/login/oauth/access_token?client_id=c624d2db46996072baa3&client_secret=b4ef1e52bb143e30e149bb426916f911111111111&code=" + code, JSONObject.class);
        System.out.println(jsonObject.toJSONString());
        JSONObject user = restTemplate.getForObject("https://api.github.com/user?access_token=" + jsonObject.get("access_token"), JSONObject.class);
        System.out.println(user.toJSONString());
        return user;
    }

}